﻿using Cuofrisp.Core.ParallelCore.Tools;
using MathNet.Symbolics;

namespace Cuofrisp.Core.ParallelCore
{
    public partial class ParallelCuofrispCalculator
    {
        private void SetInput(
            CuofrispResult result,
            SymbolicExpression[] variables, SymbolicExpression[,] metric,
            out bool containsNull)
        {
            var loopResult = Parallel.For(0, result.Metric.Length + result.VariableCount,
                this.parallelOptions,
                (iJ, loopState) => {
                    var (i, j) = iJ.As2dIndex(result.VariableCount);
                    if (i == result.VariableCount)
                    {
                        // 是针对 variables
                        if (variables[j] is null)
                        {
                            loopState.Stop();
                            return;
                        }
                        result.Variables[j] = this.simplifier.Simplify(variables[j]);
                        return;
                    }
                    else
                    {
                        // 是针对 metric
                        if (metric[i, j] is null)
                        {
                            loopState.Stop();
                            return;
                        }
                        result.Metric[i, j] = this.simplifier.Simplify(metric[i, j]);
                    }
                });
            containsNull = !loopResult.IsCompleted;
        }
    }
}